package com.tianpengtech.admin;

import java.util.HashMap;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresAuthentication;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.tianpengtech.common.GlobalConfigConstant;
import com.tianpengtech.common.exception.ServiceException;
import com.tianpengtech.common.exception.ValidateException;
import com.tianpengtech.common.util.JsonUtil;
import com.tianpengtech.common.util.XxxUtil;

@Before(InitInterceptor.class)
@RequiresAuthentication
public abstract class AbstractAdminController extends Controller {

    protected AbstractAdmin service;

    protected String indexPage;
    protected String editPage;
    protected String addPage;
    protected String detailPage;
    

    protected Class<?> clazz;

    protected abstract void config();


    public abstract void beforeAdd();

    public abstract void beforeAdd(Model<?> model) throws ValidateException;

    public abstract void beforeQueryById(Model<?> model,int id);

    public abstract void beforeUpdate(Model<?> model) throws ValidateException;

    public abstract void beforeDelete(Model<?> model);

    
    public abstract void parsePageRecord(Object results);
    /*
     * 列表页面
     */
    public void index() {

        renderTemplate(indexPage);
    }


    public void listAll() {

        int start = getParaToInt(GlobalConfigConstant.PARAM_NAME_START,
                GlobalConfigConstant.DEFAULT_VALUE_PAGENUM);
        int length = getParaToInt(GlobalConfigConstant.PARAM_NAME_LENGTH,
                GlobalConfigConstant.DEFAULT_VALUE_PAGESIZE);
        int draw = getParaToInt(GlobalConfigConstant.PARAM_NAME_DRAW, GlobalConfigConstant.DEFAULT_VALUE_PAGENUM);
        int pageNum = XxxUtil.calculatePageNumForDataTable(start, length);
        Map<String, Object> params = new HashMap<String, Object>();
        Model<?> model = (Model<?>) getModel(clazz);
        Page<?> results = (Page<?>) service.pageAllRecord(model, pageNum, length, params);
        for (Object o : results.getList()) {
        	 parsePageRecord(o);
		}
        renderJson(JsonUtil.printJsonForHPlus(draw, results));
    }

    /*
     * 	添加页面
     */
    public void add() {
        beforeAdd();
        renderTemplate(addPage);
    }

    /*
     * 保存对应的记录信息
     */
    public void doAdd() {
        try {
            Model<?> model = (Model<?>) getModel(clazz);
            beforeAdd(model);
            service.add(model);
            renderJson(JsonUtil.printSuccess(GlobalConfigConstant.TIP_MSG_SUCCESS));
        } catch (ValidateException e) {
            renderJson(JsonUtil.printError(e.getMessage()));
        } catch (ServiceException e) {
            renderJson(JsonUtil.printError(e.getMessage()));
        }
    }

    /*
     * 根据指定的ID查询对应的信息记录
     */
    public void queryById() {
        int id = getParaToInt(GlobalConfigConstant.PARAM_NAME_ID);
        Model<?> model = (Model<?>) getModel(clazz);
        model = service.queryById(model, id);
        beforeQueryById(model,id);
        setAttr(instanceName(), model);
        renderTemplate(editPage);
    }
    
    
    /*
     * 根据指定的ID查询对应的信息记录
     */
    public void showDetail() {
        int id = getParaToInt(GlobalConfigConstant.PARAM_NAME_ID);
        Model<?> model = (Model<?>) getModel(clazz);
        model = service.queryById(model, id);
        beforeQueryById(model,id);
        setAttr(instanceName(), model);
        renderTemplate(detailPage);
    }


    /*
     * 根据指定的ID更新记录信息
     */
    public void updateById() {
        try {
            Model<?> model = (Model<?>) getModel(clazz);
            beforeUpdate(model);
            service.updateById(model);
            renderJson(JsonUtil.printSuccess((GlobalConfigConstant.TIP_MSG_SUCCESS)));
        } catch (ValidateException e) {
            renderJson(JsonUtil.printError(e.getMessage()));
        } catch (ServiceException e) {
            renderJson(JsonUtil.printError(e.getMessage()));
        }
    }

    /*
     * 按照指定的ID删除对应的记录新
     */
    public void deleteById() {
        try {
            int id = getParaToInt(GlobalConfigConstant.PARAM_NAME_ID);
            Model<?> model = (Model<?>) getModel(clazz);
            model.set(GlobalConfigConstant.PARAM_NAME_ID, id);
            beforeDelete(model);
            service.deleteById(model, id);
            renderJson(JsonUtil.printSuccess(GlobalConfigConstant.TIP_MSG_SUCCESS));
        } catch (ValidateException e) {
            renderJson(JsonUtil.printError(e.getMessage()));
        } catch (ServiceException e) {
            renderJson(JsonUtil.printError(e.getMessage()));
        }
    }

    /*
     * 	获取实例的名称
     */
    protected String instanceName() {

        return StrKit.firstCharToLowerCase(clazz.getSimpleName());
    }

    /*
     * 	获取实例的名称
     */
    protected String pageName() {

        String name = instanceName();
        return name.toLowerCase();
    }
}
